/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frct_check_dead_conn.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/29 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_mng.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

int __frct_check_dead_conn(Conntrack *p_st_ct
                           , struct timeval *p_st_timeval
                           , int i_inactive_cnt);

void mylist_node_free(Frlist_node *p_st_node)
{
    if(p_st_node->data)
        frfree(p_st_node->data);
    return;
}

static void __case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    struct timeval st_timeval = {50,0};
    int i_inactive_cnt = 10;

    i_ret = __frct_check_dead_conn(p_st_ct, &st_timeval, i_inactive_cnt);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    return;
}

static void __case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    struct timeval st_timeval = {50,0};
    int i_inactive_cnt = 10;
    L7_analyzer *p_l7_analyzer = NULL;
    Frlist_node *p_st_node = NULL;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct,0x00,sizeof(Conntrack));
    p_st_ct->st_connection_lastskb_time.tv_sec = 45;
    p_st_ct->flg_report_conn_dead = FR_NO;

    p_l7_analyzer = (L7_analyzer*)frmalloc(sizeof(L7_analyzer));
    memset(p_l7_analyzer,0x00,sizeof(L7_analyzer));
    p_st_node = frlist_node_create(p_l7_analyzer);

    frlist_add_tail(&p_st_ct->lst_cap_queue,p_st_node);

    i_ret = __frct_check_dead_conn(p_st_ct, &st_timeval, i_inactive_cnt);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    frlist_remove_all((Frlist_header *)&p_st_ct->lst_cap_queue, mylist_node_free);
    frfree(p_st_ct);
    return;
}

static void __case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    struct timeval st_timeval = {50,0};
    int i_inactive_cnt = 10;
    L7_analyzer *p_l7_analyzer = NULL;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct,0x00,sizeof(Conntrack));
    p_st_ct->st_connection_lastskb_time.tv_sec = 40;
    p_st_ct->flg_report_conn_dead = FR_NO;

    p_l7_analyzer = &p_st_ct->st_l7_analyzer;
    p_l7_analyzer->p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));



    i_ret = __frct_check_dead_conn(p_st_ct, &st_timeval, i_inactive_cnt);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_l7_analyzer->p_st_l7_module);
    frfree(p_st_ct);
    return;
}




int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frct_check_dead_conn 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", __case1);
   	CU_add_test(pSuite, "case2", __case2);
   	CU_add_test(pSuite, "case3", __case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}